package com.sgb.kjwl.viewmodel.machine;

import androidx.databinding.ObservableField;
import android.util.Log;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseDetailEditEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseItemEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseSheBeiEntity;
import com.sgb.kjwl.model.entity.lease.AskLeaseSheBeiEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.view.ui.activity.leaseAsk.AskLeasePubEditActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AskLeasePubEditModel extends BaseViewModel {

    private AskLeasePubEditActivity askLeaseActivity;
    private MachineRepertory repertory;

    public ObservableField<AskLeaseSheBeiEntity> addDeviceEntity = new ObservableField<>(new AskLeaseSheBeiEntity());
    public ObservableField<AskLeaseSheBeiEntity3> addDeviceEntity1 = new ObservableField<>(new AskLeaseSheBeiEntity3());
    public AskLeasePubEditModel(AskLeasePubEditActivity askLeaseActivity, MachineRepertory repertory){
        this.askLeaseActivity = askLeaseActivity;
        this.repertory = repertory;
    }

    public void pubMatchData(){
        DialogHelper.showProgressDialog(askLeaseActivity, "提交中", 0, false);
        APIManager.getInstance().getMachineAPI().pubMatch(addDeviceEntity1.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(askLeaseActivity, "提交成功");
                    askLeaseActivity.finish();
                } else {
                    MToast.showToast(askLeaseActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void pubDeviceData(){
        DialogHelper.showProgressDialog(askLeaseActivity, "提交中", 0, false);
        APIManager.getInstance().getMachineAPI().pubDevice(addDeviceEntity1.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    MToast.showToast(askLeaseActivity, "提交成功");
                    askLeaseActivity.finish();
                } else {
                    MToast.showToast(askLeaseActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTypeModel(){
        APIManager.getInstance().getMachineAPI().getAllTypeList().enqueue(new Callback<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Response<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if(null!=response.body().getData()){
                        askLeaseActivity.showTypeView(response.body().getData());
                    }else{
                        MToast.showToast(askLeaseActivity,"暂无数据");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity<List<AskLeaseItemEntity>>>>>>> call, Throwable t) {
                Log.v("dd","sdsdd");
            }
        });
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(askLeaseActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    askLeaseActivity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(askLeaseActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    askLeaseActivity.displayRole(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getMatchDetail(String id){
        DialogHelper.showProgressDialog(askLeaseActivity, "提交中", 0, false);
        APIManager.getInstance().getMachineAPI().getMatchDetail(repertory.getAreaCode(),id).enqueue(new Callback<BaseEntity<AskLeaseDetailEditEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AskLeaseDetailEditEntity>> call, Response<BaseEntity<AskLeaseDetailEditEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    askLeaseActivity.showDetail(response.body().getData());
                } else {
                    MToast.showToast(askLeaseActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AskLeaseDetailEditEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

//    public void getDeviceDetail(String id,String type){
//        DialogHelper.showProgressDialog(askLeaseActivity, "提交中", 0, false);
//        APIManager.getInstance().getMachineAPI().getDeviceDetail(repertory.getAreaCode(),id,type).enqueue(new Callback<BaseEntity<AskLeaseSheBeiEntity3>>() {
//            @Override
//            public void onResponse(Call<BaseEntity<AskLeaseSheBeiEntity3>> call, Response<BaseEntity<AskLeaseSheBeiEntity3>> response) {
//                DialogHelper.dismissProgressDialog();
//                if (response.body() == null || response.body().getState() == null) {
//                    return;
//                }
//                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
//                    askLeaseActivity.showDetail(response.body().getData());
//                } else {
//                    MToast.showToast(askLeaseActivity, response.body().getMsg());
//                }
//            }
//
//            @Override
//            public void onFailure(Call<BaseEntity<AskLeaseSheBeiEntity3>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
//            }
//        });
//    }
}
